'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if (event.leixing == 1) { 
		if(event.guanlilaoshiid==''){
			const res=await db.collection('xiaoqu').get()
			return res
		}else {
			const res=await db.collection('xiaoqu').where({
				guanlilaoshiid:event.guanlilaoshiid
			}).get()
			return res
		}
	} else if (event.leixing == 2) { 
		// const res = await db.collection('kecheng').where({
		// 	laoshiid: event.id
		// }).get()
		const res = await db.collection('kecheng').where({
			name: new RegExp(event.key)
		}).get()
		return res
	} else if (event.leixing == 3) { 
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ 
			
			role: ['admin'], 
			
		})
		const order = await dbJQL.collection('baokelist').getTemp() 
		const book = await dbJQL.collection('uni-id-users').field(
				'_id,name,avatar,age,nianji,school,mobile,zhuzhi')
			.getTemp() 
		const res = await dbJQL.collection(order, book).where({
			kechengid: event._id,
			zhuangtai: 1
		}).get() 
		return res
	} else if (event.leixing == 4) {
		const res1 = await db.collection('xiaokelist').add(event.hexiaolist)
		const res2 = await db.collection('baokelist').where({
			_id: dbCmd.in(event.checkedArr)
		}).update({
			yihexiaoshu: dbCmd.inc(1),
			weixiaokeshi:dbCmd.inc(-1),
			kexiaoendtime: event.kexiaoendtime
		})
		const res3 = await db.collection('baokelist').where({
			_id: dbCmd.in(event.hexiaolist1)
		}).update({
			zhuangtai: 0
		})
		const res4 = await db.collection('baokelist').where({
			xufeifrom: dbCmd.in(event.hexiaolist1)
		}).update({
			zhuangtai: 1
		})
	} else if (event.leixing == 5) { 
		const res = await db.collection('xiaokelist').where({
			baokeid: event.baokeid
		}).orderBy('xiaoketime', 'desc').get()
		return res
	} else if (event.leixing == 7) { 
		const res = await db.collection('jiaoshi').doc(event.id).update({
			name: event.name
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '修改教室数据',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 8) { 
		const res = await db.collection('jiaoshi').add({
			name: event.name,
			guanlilaoshi: event.guanlilaoshi,
			guanlilaoshidianhua: event.guanlilaoshidianhua,
			guanlilaoshiid: event.guanlilaoshiid,
			weizhi: event.weizhi
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '修改教室数据',
			xiaoqu: event.weizhi
		})
		return res
	} else if (event.leixing == 9) { 
		const res = await db.collection('jiaoshi').doc(event._id).remove()
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '删除教室数据',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 10) { 
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		const dbCmd_ = dbJQL.command
		dbJQL.setUser({ 
			
			role: ['admin'], 
			
		})
		const res = await dbJQL.collection('uni-id-users').where(
		dbCmd_.or({
			role: event.role,
			mobile: new RegExp(event.keyword)
		} , {
			role: event.role,
			name: new RegExp(event.keyword)
		})
		).field('mobile,name,role,avatar').get()
		return res
	} else if (event.leixing == 11) { 
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ 
			
			role: ['admin'], 
			
		})
		const res = await dbJQL.collection('uni-id-users').where(
			'role=="jiaowulaoshi" || role=="renkelaoshi"'
		) .field('mobile,name,role,avatar').get()
		return res
	} else if (event.leixing == 14) { 
		const res = await db.collection('uni-id-users').where({
			mobile: event.tel
		}).get()
		return res
	} else if (event.leixing == 15) { 
		const res = await db.collection('fengcaizhanshi').doc(event._id).remove()
		return res
	} else if (event.leixing == 16) { 
		const res = await db.collection('maihuomingxi').add(event.mingxi)
		const res1 = await db.collection('zhangwuliushui').add({
			jine: event.jine,
			biandong: event.biandong,
			leibie: event.leibie,
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu
		})
		const res2 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: event.leibie,
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 17) { 
		const res = await db.collection('shangpin').doc(event._id).update({
			kucunliang: event.kucunliang
		})
		return res
	} else if (event.leixing == 18) { 
		const res = await db.collection('maihuomingxi').where({
			xiaoqu: dbCmd.in(event.xiaoqu)
		}).get()
		return res
	} else if (event.leixing == 19) { 
		if (event.shenfen == '管理员') {
			const res = await db.collection('zhangwuliushui').where({
					xiaoqu: dbCmd.in(event.weizhi)
				})
				.orderBy('riqi', 'desc').limit(event.tiaoshu).skip(event.skip).get()
			return res
		} else {
			const res = await db.collection('zhangwuliushui').where({
				jingshouren: event.jingshouren,
				xiaoqu: dbCmd.in(event.weizhi)
			}).orderBy('riqi', 'desc').get()
			return res
		}
	} else if (event.leixing == 20) { 
		const res = await db.collection('zhangwuliushui').add({
			jine: event.jine,
			biandong: event.biandong,
			leibie: event.leibie,
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			beizhu: event.beizhu
		})
		return res
	} else if (event.leixing == 21) { 
		const res = await db.collection('uni-id-users').doc(event._id).update({
			mobile: event.tel,
			username:event.tel,
			name: event.name,
			born_day: event.born_day,
			zhuzhi: event.zhuzhi,
			school: event.school,
			nianji: event.nianji,
			avatar: event.zhaopian,
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: event.leibie,
		})
		const res2 = await db.collection('baokelist').doc(event.baokeid).update({
			baomingdate: event.baomingdate,
			jiezhidate: event.jiezhidate,
			yihexiaoshu: event.yihexiaoshu,
			beizhu: event.beizhu,
			weixiaokeshi:event.kechengshu-event.yihexiaoshu
		})
		return res
	} else if (event.leixing == 22) { 
		const res = await db.collection('xiugaishuju').orderBy('riqi', 'desc').limit(50).skip(event.skip).get()
		return res
	} else if (event.leixing == 23) { 
		const res = await db.collection('jiaoshi').where({
			weizhi: dbCmd.in(event.weizhi),
			name: new RegExp(event.key)
		}).get()
		return res
	} else if (event.leixing == 24) { 
		const res = await db.collection('kecheng').doc(event.id).update({
			name: event.name,
			laoshi: event.laoshi,
			laoshitel: event.laoshitel,
			fenleiid:event.fenleiid,
			laoshiid: event.laoshiid,
			keshifeitolaoshi: event.keshifeitolaoshi,
			jiaowulaoshi: event.jiaowulaoshi
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '修改课程数据',
			xiaoqu: event.xiaoqu
		})
		const res2 = await db.collection('baokelist').where({
			kecheng: event.yuankecheng,
		}).update({
			kecheng: event.name
		})
		return res
	} else if (event.leixing == 25) { 
		const res = await db.collection('paike').add({
			xiaoqu: event.xiaoqu,
			jiaoshi: event.jiaoshi,
			kecheng: event.kecheng,
			monthday: event.monthday,
			qishitime: event.qishitime,
			jieshutime: event.jieshutime,
			jingshouren: event.jingshouren,
			paikelaiyuanid: event.paikelaiyuanid,
			create_time:event.riqi
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '添加教室排课',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 26) { 
		const res1 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周一",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res2 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周二",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res3 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周三",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res4 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周四",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res5 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周五",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res6 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周六",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		const res7 = await db.collection('paike').where({
			xiaoqu: event.xiaoqu,
			monthday: "周日",
			jiaoshi: event.jiaoshi,
			// paikelaiyuanid: event.paikelaiyuanid
		}).orderBy('qishitime', 'asc').get()
		return {
			res1,
			res2,
			res3,
			res4,
			res5,
			res6,
			res7
		}
	} else if (event.leixing == 27) { 
		const res = await db.collection('kecheng').where({
			xiaoqu: dbCmd.in(event.xiaoqu),
			name: new RegExp(event.key)
		}).orderBy('name', 'desc').get()
		return res
	} else if (event.leixing == 28) { 
		const res = await db.collection('paike').doc(event._id).update({
			kecheng: event.kecheng,
			qishitime: event.qishitime,
			jieshutime: event.jieshutime,
			jingshouren: event.jingshouren,
			monthday: event.monthday
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '修改教室排课',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 29) { 
		const res = await db.collection('paike').doc(event._id).remove()
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '删除教室排课',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 30) { 
		const res = await db.collection('laoshi').get()
		return res
	} else if (event.leixing == 31) { 
		const res = await db.collection('shangpin').add({
			name: event.name,
			jieshao: event.jieshao,
			zhaopian: event.zhaopian,
			kucunliang: event.kucunliang,
			jiage: event.jiage,
			xiaoqu: event.xiaoqu,
			shangpin_jinjia:event.shangpin_jinjia,
			fenlei_id: event.fenlei_id,
			jingshouren: event.jingshouren,
			creat_time: event.riqi,
			beizhu: ''
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			leibie: '添加商品数据',
			xiaoqu: event.xiaoqu
		})
		return res
	} else if (event.leixing == 32) { 
		const res = await db.collection('shangpin').doc(event.id).remove()
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: '删除商品数据'
		})
		return res
	} else if (event.leixing == 33) { 
		const res = await db.collection('shangpin_fenlei').get()
		return res
	} else if (event.leixing == 34) { 
		const res = await db.collection('shangpin_fenlei').doc(event.id).update({
			update_time: event.time,
			name: event.name,
			jingshouren: event.jingshouren

		})
		return res
	} else if (event.leixing == 35) { 
		const res = await db.collection('shangpin_fenlei').add({
			creat_time: event.time,
			name: event.name,
			jingshouren: event.jingshouren

		})
		return res
	} else if (event.leixing == 36) { 
		const res = await db.collection('shangpin_fenlei').doc(event.id).remove()
		return res
	} else if (event.leixing == 37) { 
		const res = await db.collection('shangpin').doc(event._id).update({
			name: event.name,
			jieshao: event.jieshao,
			zhaopian: event.zhaopian,
			kucunliang: event.kucunliang,
			jiage: event.jiage,
			xiaoqu: event.xiaoqu,
			fenlei_id: event.fenlei_id,
			jingshouren: event.jingshouren,
			shangpin_jinjia:event.shangpin_jinjia,
			update_time: event.riqi,
			beizhu: ''
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: '修改商品数据'
		})
		return res
	}else if (event.leixing == 38) {
		const res = await db.collection('kecheng_fenlei').get()
		return res
	}else if (event.leixing == 39) {
		const res = await db.collection('kecheng_fenlei').doc(event.id).update({
			update_time:event.time,
			name:event.name,
			jingshouren:event.jingshouren
			
		})
		return res
	}else if (event.leixing == 40) {
		const res = await db.collection('kecheng_fenlei').add({
			creat_time:event.time,
			name:event.name,
			jingshouren:event.jingshouren
			
		})
		return res
	}else if (event.leixing == 41) {
		const res = await db.collection('kecheng_fenlei').doc(event.id).remove()
		return res
	}else if (event.leixing == 42) {
		const res = await db.collection('kecheng_fenlei').get()
		return res
	}else if (event.leixing == 43) { 
		const res = await db.collection('kecheng').get()
		return res
	}

};